package com.eii.resubmit.controller;

import com.eii.resubmit.aop.request.annotation.LimitResquest;
import com.eii.resubmit.aop.request.annotation.RepeatLock;
import com.eii.resubmit.aop.request.annotation.RepeatRedisLock;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author eii
 * @sice 2020/11/28
 */
@Slf4j
@RestController
@RequestMapping("/sys")
public class MainController {

    @RepeatLock
    @GetMapping("/state")
    public Map<String, String> state() {
        Map<String, String> resMap = new HashMap<>();
        resMap.put("result", "ok:" + System.currentTimeMillis());
        return resMap;
    }

    @SneakyThrows
    @RepeatLock
    @GetMapping("/test")
    public Map<String, String> test() {
        log.info("test enter");
        Map<String, String> resMap = new HashMap<>();
        resMap.put("test", "ok:" + System.currentTimeMillis());
        Thread.sleep(3000);
        log.info("test ok");
        return resMap;
    }


    @SneakyThrows
    @RepeatRedisLock
    @GetMapping("/testredis")
    public Map<String, String> testredis() {
        log.info("testredis enter");
        Map<String, String> resMap = new HashMap<>();
        resMap.put("testredis", "ok:" + System.currentTimeMillis());
        Thread.sleep(3000);
        log.info("testredis ok");
        return resMap;
    }

    @SneakyThrows
    @LimitResquest(value = 0.3, timeout = 1000)
    @GetMapping("/testbusy")
    public Map<String, String> testbusy() {
        log.info("testbusy enter");
        Map<String, String> resMap = new HashMap<>();
        resMap.put("testbusy", "ok:" + System.currentTimeMillis());
        log.info("testbusy ok");
        return resMap;
    }
}
